<?php

namespace App\Http\Traits;


use Illuminate\Http\Response;

trait ApiResponse
{

    /**
     * 接口返回通用数据结构
     * @param array|mixed $data
     * @param int $status [0:success]
     * @param string $message
     * @return Response
     */
    protected function renderJson($data = [], $status = 0, $message = 'success')
    {
        $result = [
            'status' => $status,
            'message' => $message,
            'data' => $data
        ];

        return $this->send($result, 200);
    }


    /**
     * 互推圈专用接口返回
     * @param array $data
     * @param int $status
     * @param string $message
     * @return Response
     */
    protected function InterPushRenderJson($data = [], $status = 0, $message = 'success', $page=[])
    {
        $result = [
            'status' => $status,
            'message' => $message,
            'data' => $data,
            'page' => $page
        ];

        return $this->send($result, 200);
    }

    /**
     * 返回成功
     * @param string $message
     * @param int $code
     * @return \Illuminate\Http\Response
     */
    protected function success($message = 'success', $code = 200)
    {
        $result = [
            'status' => 0,
            'message' => $message,
            'data' => []
        ];
        return $this->send($result, $code);
    }

    /**
     * 失败
     * @param $message
     * @param int $code
     * @return \Illuminate\Http\Response
     */
    protected function fail($message, $code = 200)
    {
        $result = [
            'status' => -1,
            'message' => $message,
            'data' => []
        ];

        return $this->send($result, $code);
    }

    /**
     * 发生json数据
     * @param $data
     * @param $code
     * @return \Illuminate\Http\Response
     */
    protected function send($data, $code = 200)
    {
        return response()->json($data, $code, ['Content-Type'=>'application/json; charset=UTF-8']);
    }

    /**
     * 服务器错误
     * @param $message
     * @return \Illuminate\Http\Response
     */
    protected function error($message = '系统错误,请稍后重试!')
    {
        $result = [
            'status' => -1,
            'message' => $message,
            'data' => []
        ];

        return $this->send($result, 500);
    }
}